package com.m3958.logcollector.tree;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class TreeNode<T> implements Iterable<TreeNode<T>>{
    
    private long id;
    
    @JsonProperty("n")
    private String name;

    @JsonProperty("v")
    private T nodeValue;

    @JsonProperty("c")
    private List<TreeNode<T>> children = new ArrayList<>();
    
    @JsonProperty("p")
    private TreeNode<T> parent;

    public TreeNode() {
    };

    public TreeNode(long id, String name) {
        this.id = id;
        this.name = name;
    };
    
    public void addChild(TreeNode<T> child) {
        child.setParent(this);
        this.children.add(child);
    }
    

    public T getNodeValue() {
        return nodeValue;
    }

    public void setNodeValue(T nodeValue) {
        this.nodeValue = nodeValue;
    }

    public TreeNode<T> getParent() {
        return parent;
    }

    public void setParent(TreeNode<T> parent) {
        this.parent = parent;
    }

    public List<TreeNode<T>> getChildren() {
        return children;
    }

    public void setChildren(List<TreeNode<T>> children) {
        this.children = children;
    }

    @Override
    public Iterator<TreeNode<T>> iterator() {
        return children.iterator();
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
